package com.archgeek.java.conc;

import java.util.concurrent.TimeUnit;

/**
 * @author pizhihui
 * @date 2024-03-20 15:12
 */
public class DeadLockDemo {


    static Object lock1 = new Object();
    static Object lock2 = new Object();

    public static void main(String[] args) {


        new Thread(() -> {
            synchronized (lock1) {
                sleep(1000);
                System.out.println(Thread.currentThread().getName() + "lock1干完活");
                synchronized (lock2) {
                    sleep(2000);
                    System.out.println(Thread.currentThread().getName() + "lock2干完活");
                }
            }

        }, "a").start();

        new Thread(() -> {
            synchronized (lock2) {
                sleep(1000);
                System.out.println(Thread.currentThread().getName() + "lock2干完活");
                synchronized (lock1) {
                    sleep(2000);
                    System.out.println(Thread.currentThread().getName() + "lock1干完活");
                }
            }

        }, "b").start();


    }
    private static void sleep(long milli) {
        try {
            TimeUnit.MILLISECONDS.sleep(milli);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
